package com.fenghuajueli.libbasecoreui.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NumsUtils {


    /**
     * 返回保留两位小数的结果，多余的位数舍弃，不五入
     *
     * @param num
     * @return
     */
    public static double formatTwoDecimal(double num) {
        BigDecimal bg = new BigDecimal(num).setScale(2, RoundingMode.DOWN);
        return bg.doubleValue();
    }


    /**
     * 返回保留两位小数的结果，多余的位数舍弃，不五入
     *
     * @param num
     * @return
     */
    public static float formatThreeDecimal(float num) {
        BigDecimal bg = new BigDecimal(num).setScale(3, RoundingMode.DOWN);
        return bg.floatValue();
    }



    /**
     * 返回保留两位小数的结果，多余的位数舍弃，不五入
     *
     * @param num
     * @return
     */
    public static float formatTwoDecimal(float num) {
        BigDecimal bg = new BigDecimal(num).setScale(2, RoundingMode.DOWN);
        return bg.floatValue();
    }

    /**
     * 返回保留一位小数的结果，多余的位数舍弃，不五入
     *
     * @param num
     * @return
     */
    public static double formatOneDecimal(double num) {
        BigDecimal bg = new BigDecimal(num).setScale(1, RoundingMode.UP);
        return bg.doubleValue();
    }


    // 判断一个字符串是否含有数字
    public boolean HasDigit(String content) {
        boolean flag = false;
        Pattern p = Pattern.compile(".*\\d+.*");
        Matcher m = p.matcher(content);
        if (m.matches()) {
            flag = true;
        }
        return flag;
    }


    public static List<String> getNumbers(String content) {
        List<String> data = new ArrayList<>();
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            data.add(matcher.group());
        }
        return data;
    }

    // 截取非数字
    public static String splitNotNumber(String content) {
        Pattern pattern = Pattern.compile("\\D+");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return matcher.group(0);
        }
        return "";
    }
}
